const Router=require("koa-router");
const api=require("../mock");
const router=new Router();

router.get("/",async function(ctx){
    ctx.body="welcome";
});

router.get("/user/list",async function(ctx){
    ctx.body = {
        state:true,
        data:api.findUserList().list
    }
    // ctx.body = {
    //     state:false,
    //     data:null,
    //     msg:"未授权"
    // }
})

// router.delete("/test/full",async function(ctx){
//     ctx.body = {
//         state:true,
//         data:"请求成功:"+ctx.method
//     }
// })
router.all("/test/full",async function(ctx){
    ctx.body = {
        state:true,
        data:"请求成功="+ctx.method,
        query:ctx.query, // URL地址参数
        body:ctx.request.body // koa-body 完成请求体参数解析和获取
    }
})

router.get("/stu",async function(ctx){
    ctx.body = {
        msg:"查询列表成功",
        state:true,
        data:api.findUserList().list
    }
})
// 构建rest参数接口
router.get("/stu/:id",async function(ctx){
    // 获取rest参数
    let rest = ctx.params;
    ctx.body = {
        msg:"查询列表成功",
        state:true,
        data:api.findUserById(rest.id)
    }
})
router.post("/stu",async function(ctx){
    ctx.request.body.id = api.Random.id()
    ctx.body = {
        msg:"添加新用户成功",
        state:true,
        data:ctx.request.body
    }
})
router.patch("/stu",async function(ctx){
    ctx.body = {
        msg:"修改用户信息成功",
        state:true,
        data:ctx.request.body
    }
})
router.delete("/stu",async function(ctx){
    ctx.body = {
        msg:"删除用户成功",
        state:true,
        data:ctx.query
    }
})

router.get("/student",async function(ctx){
    let id = ctx.query.id;
    ctx.body = api.findStudentById(id)
})


// 不带跨域功能的api接口
router.get("/cors/list",async function(ctx){
    ctx.body = {
        state:true,
        data:api.findUserList().list
    }
})


router.all(/\/.*/,async function(ctx){
    ctx.status = 404;
    ctx.body = {
        state:false,
        msg:"网络资源不存在"
    }
});



exports.router=router;